﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;

namespace Alfa.Common.ValidationRules {
	public class StringGramarValidationRule : ValidationRuleBase {
		private bool _numbers;
		private bool _lowerCharacters;
		private bool _upperCharacters;
		private bool _specialCharacters;
		private bool _whitespaceCharacters;

		Regex regexNumbers;
		Regex regexLowerCharacters;
		Regex regexUpperCharacters;
		Regex regexSpecialCharacters;
		Regex regexWhitespaceCharacters;

		public StringGramarValidationRule( ) {
			regexNumbers = new Regex("[0-9]+");
			regexLowerCharacters = new Regex("[a-z]+");
			regexUpperCharacters = new Regex("[A-Z]+");
			regexSpecialCharacters = new Regex("[^A-Za-z0-9 \t\r\n\v\f]+");
			regexWhitespaceCharacters = new Regex("[ \t\r\n\v\f]+");
		}

		public bool Numbers {
			get { return _numbers; }
			set { _numbers = value; }
		}

		public bool LowerCharacters {
			get { return _lowerCharacters; }
			set { _lowerCharacters = value; }
		}

		public bool UpperCharacters {
			get { return _upperCharacters; }
			set { _upperCharacters = value; }
		}

		public bool SpecialCharacters {
			get { return _specialCharacters; }
			set { _specialCharacters = value; }
		}

		public bool WhitespaceCharacters {
			get { return _whitespaceCharacters; }
			set { _whitespaceCharacters = value; }
		}

		public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
			var textValue = ((string)value);

			if (!Numbers && regexNumbers.IsMatch(textValue)) {
				return this.ParseError(false, Messages.NumbersAreIllegal + ".");
			} else if (!LowerCharacters && regexLowerCharacters.IsMatch(textValue)) {
				return this.ParseError(false, Messages.LowerCharactersAreIllegal + ".");
			} else if (!UpperCharacters && regexUpperCharacters.IsMatch(textValue)) {
				return this.ParseError(false, Messages.UpperCharactersAreIllegal + ".");
			} else if (!WhitespaceCharacters && regexWhitespaceCharacters.IsMatch(textValue)) {
				return this.ParseError(false, Messages.WhitespaceCharactersAreIllegal + ".");
			} else if (!SpecialCharacters && regexSpecialCharacters.IsMatch(textValue)) {
				return this.ParseError(false, Messages.SpecialCharactersAreIllegal + ".");
			} else {
				return this.ParseError(true, null);
			}
		}

	}
}
